<template>
  <div>
    <el-dialog
      :visible="addDialog"
      width="50%"
      :before-close="closeDialog"
      top="5vh"
      :close-on-click-modal="false"
    >
      <div class="dialogTitle" slot="title">{{ this.form.id ? "编辑车辆" : "新增车辆" }}</div>
      <el-form :model="form" label-width="120px">
        <el-form-item label="车牌号:">
          <el-input
            v-model="form.carLicenseId"
            autocomplete="off"
            placeholder="请输入 "
          />
        </el-form-item>
        <el-form-item label="驾驶员:">
          <el-input
            v-model="form.carDriver"
            autocomplete="off"
            placeholder="请输入 "
          />
        </el-form-item>
        <el-form-item label="车辆照片:">
          <el-upload
            action=""
            :http-request="UploadImage3"
            :file-list.sync="fileList"
            :limit="1"
            list-type="picture-card"
            :on-exceed="handleExceed"
            :on-change="handleFileChange"
          >
            <i v-if="fileList.length < 1" slot="default" class="el-icon-plus" />
            <div slot="file" slot-scope="{ file }">
              <img
                class="el-upload-list__item-thumbnail"
                :src="file.url"
                alt=""
              />
              <span class="el-upload-list__item-actions">
                <span
                  class="el-upload-list__item-delete"
                  @click="handleRemove(file)"
                >
                  <i class="el-icon-delete" />
                </span>
              </span>
            </div>
          </el-upload>
        </el-form-item>

        <el-form-item label="车辆状态:">
          <el-select v-model="form.carStatus" placeholder="请选择车辆状态">
            <el-option
              v-for="item in options1"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="车辆类型:">
          <el-select v-model="form.carType" placeholder="请选择车辆类型">
            <el-option
              v-for="item in options2"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="车辆归属公司:">
          <el-input
            v-model="form.company"
            autocomplete="off"
            placeholder="请输入 "
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="!form.id" class="addBtn" type="primary" @click="submit"
          >确定新增</el-button
        >
        <el-button v-else class="addBtn" type="primary" @click="submit"
          >确定修改</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { uploadApi } from "@/api/login";
import {
  vehicleAddApi,
  getVehicle,
  vehicleUpdateApi,
} from "@/api/vehicleTable";

export default {
  name: "addProblem",
  props: {
    addDialog: {
      type: Boolean,
      default: false,
    },
    dialogId: {
      type: String,
      default: null,
    },
  },
  components: {},
  data() {
    return {
      form: {
        carLicenseId: "",
        carDriver: "",
        carStatus: "",
        carType: "",
        company: "",
      },
      fileList: [],
      options1: [
        {
          value: 0,
          label: "正常",
        },
        {
          value: 1,
          label: "维修中",
        },
        {
          value: 2,
          label: "报废",
        },
      ],
      options2: [
        {
          value: 0,
          label: "行政车辆",
        },
        {
          value: 1,
          label: "工作车辆",
        },
      ],
      uploaderSubmit: [],
    };
  },
  created() {},
  methods: {
    async submit() {
      if (
        this.form.carLicenseId == "" ||
        this.form.carDriver == "" ||
        this.form.carStatus === "" ||
        this.form.carType === "" ||
        this.form.company == ""
      ) {
        this.$message.warning("请填写完整信息");
        return;
      }
      if (this.form.id) {
        const res = await vehicleUpdateApi({
          id: this.form.id,
          carLicenseId: this.form.carLicenseId,
          carDriver: this.form.carDriver,
          carStatus: this.form.carStatus,
          carType: this.form.carType,
          company: this.form.company,

          carImage: this.uploaderSubmit[0].url,
        });

        if (res.code == 0) {
          this.$message.success("修改成功");
          this.closeDialog();
          this.$parent.vehiclePage();
        } else {
          this.$message.error("新增失败");
        }
      } else {
        const res = await vehicleAddApi({
          carLicenseId: this.form.carLicenseId,
          carDriver: this.form.carDriver,
          carStatus: this.form.carStatus,
          carType: this.form.carType,
          company: this.form.company,

          carImage: this.uploaderSubmit[0].url,
        });

        if (res.code == 0) {
          this.$message.success("新增成功");
          this.closeDialog();
          this.$parent.vehiclePage();
        } else {
          this.$message.error("新增失败");
        }
      }
    },
    closeDialog() {
      this.form = {};
      this.fileList = [];
      this.uploaderSubmit = [];

      this.$emit("update:addDialog", false); // 触发父组件更新
    },

    handleFileChange(file, fileList) {
      this.fileList = fileList; // 手动同步
    },
    handleRemove(e) {
      this.uploaderSubmit = this.uploaderSubmit.filter((item) => {
        return item.uid !== e.uid;
      });
      this.fileList = this.fileList.filter((item) => {
        return item.uid !== e.uid;
      });
    },
    handleExceed() {
      this.$message.warning("最多上传 1 个文件");
    },
    async UploadImage3(file) {
      this.openLoading();
      const formData = new FormData();
      formData.append("file", file.file);
      const res = await uploadApi(formData);
      if (res.code === 0) {
        this.uploaderSubmit.push({
          url: res.data,
          name: file.file.name,
          uid: file.file.uid,
        });
      } else {
        this.fileList.pop();
      }
      this.openLoading().close();
    },
    async getDetail(id) {
      const res = await getVehicle({ id });
      this.form = res.data;
      this.fileList = [
        {
          url: res.data.carImage,
        },
      ];
      this.uploaderSubmit = [
        {
          url: res.data.carImage,
        },
      ];
    },
  },
  mounted() {},
  watch: {},
  computed: {},
  filters: {},
};
</script>

<style scoped lang="scss">
.dialogTitle {
  position: relative;
  padding-left: 10px; /* 调整文本与颜色条之间的间距 */
  font-weight: bold;
}
.dialogTitle::before {
  content: "";
  position: absolute;
  left: 0;
  top: 50%;
  border-radius: 2px;
  transform: translateY(-50%);
  width: 4px; /* 颜色条的宽度 */
  height: 14px; /* 颜色条的高度 */
  background-color: #0077FF; /* 颜色条的颜色 */
}
.el-select {
  width: 100%;
}
::v-deep .el-dialog__body {
  padding-right: 50px !important;
}
::v-deep .dialog-footer {
  text-align: center;
  .addBtn {
    background: #0077FF;
    border-radius: 4px 4px 4px 4px;
    border: none;
    color: #fff;
  }
}
</style>
